package com.yang.paymentdemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yang.paymentdemo.entity.OrderInfo;
import com.yang.paymentdemo.entity.Product;
import com.yang.paymentdemo.enums.OrderStatus;
import com.yang.paymentdemo.mapper.OrderInfoMapper;
import com.yang.paymentdemo.mapper.ProductMapper;
import com.yang.paymentdemo.service.OrderInfoService;
import com.yang.paymentdemo.util.OrderNoUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Duration;
import java.time.Instant;
import java.util.List;

@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Resource
    private ProductMapper productMapper;

//    @Resource
//    private OrderInfoMapper orderInfoMapper;

    @Override
    public OrderInfo createOrderByProductId(Long productId) {

        OrderInfo orderInfo = this.getNoPayOrderByProductId(productId);
        if(orderInfo != null){
            return orderInfo;
        }

        //获取商品信息
        Product product = productMapper.selectById(productId);

        //生成订单
        orderInfo = new OrderInfo();
        orderInfo.setTitle(product.getTitle());
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());//获取订单编号
        orderInfo.setProductId(productId); //商品编号
        orderInfo.setTotalFee(product.getPrice()); //总金额，单位：分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType()); //订单状态，未支付
        baseMapper.insert(orderInfo);   //baseMapper在这里等同于OrderInfoMapper

        return orderInfo;
    }

    /**
     * 存储订单二维码
     * @param orderNo
     * @param codeUrl
     */
    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);

        baseMapper.update(orderInfo, queryWrapper);
    }

    /**
     * 查询订单列表，并倒叙查询
     * @return
     */
    @Override
    public List<OrderInfo> listOrderByCreateTimeDesc() {

        //QueryWrapper支持链式操作写法，创建对象时，后面的泛型要补全，否则会报错
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<OrderInfo>()
                .orderByDesc("create_time");
        return baseMapper.selectList(queryWrapper);

    }

    /**
     * 根据订单号更新订单状态
     * @param orderNo
     * @param orderStatus
     */
    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {
        log.info("更新订单状态 ===> {}",orderStatus.getType());

        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus.getType());

        baseMapper.update(orderInfo,queryWrapper); //将数据存进数据库中

    }

    /**
     * 获取订单状态
     * @param orderNo
     * @return
     */
    @Override
    public String getOrderStatus(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        if(orderInfo == null){
            return null;
        }
        return orderInfo.getOrderStatus();

    }

    /**
     * 查询创建超过minutes分钟 并且 未支付的订单
     * @param minutes
     * @return
     */
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes) {

        //创建instant实例：当前时间减去minutes分钟，=> minutes分钟前的实例
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));
        log.warn("当前时间 ===>{}", Instant.now());
        //当前时间 ===>2022-02-10T14:58:00.005Z
        log.warn("5分钟前的时间 ===>{}", instant);


        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
        queryWrapper.le("create_time",instant);

        List<OrderInfo> orderInfoList = baseMapper.selectList(queryWrapper);
        return orderInfoList;

    }

    /**
     * 根据订单号获取订单
     * @param orderNo
     * @return
     */
    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);

        return orderInfo;
    }

    /**
     * 根据商品id查询未支付订单
     * 防止重复创建订单对象
     * @param productId
     * @return
     */
    private OrderInfo getNoPayOrderByProductId(Long productId) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>(); //mybatisPlus下的条件拼接器
        queryWrapper.eq("product_id",productId);
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());
//        queryWrapper.eq("user_id",userId);
        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }
}
